package com.sqkj.controller;

import com.sqkj.entity.Help_other;
import com.sqkj.entity.Help_other_User;
import com.sqkj.entity.User;
import com.sqkj.service.Help_otherService;
import com.sqkj.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.naming.Name;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/help_other")
public class Help_otherController {
    @Autowired
    Help_otherService service;
    @Autowired
    UserService userService;
    @GetMapping("/add")
    public Map<String, Object> addHelo_other(Help_other help_other) {
        Map<String, Object> map = new HashMap<String, Object>();

        if (isNull(help_other)) {
            map = service.add(help_other);
            return map;
        } else {
            map.put("isSuccess", false);
            return map;
        }
    }
    @RequestMapping("/findBySuccessStatus")
    public List<Help_other_User> findBySuccessStatus() {
        List<Help_other> others = service.findBySuccessStatus();
        List<Help_other_User> help_other_users = new ArrayList<>();
        Help_other_User help_other_user = null;
        for (Help_other help_other:others) {
            User user = userService.findById(help_other.getUserId());
            help_other_user = new Help_other_User(help_other,user);
            help_other_users.add(help_other_user);
        }
        return help_other_users;
    }

    public Boolean isNull(Help_other help_other) {
        int hUserId = help_other.getUserId();
        String hName = help_other.getName();
        String hSex = help_other.getSex();
        String homeTown = help_other.getHometown();
        String hID = help_other.getIDCard();
        String hPhone = help_other.getPhone();
        String hAddress = help_other.getAddress();
        String hReason = help_other.getReason();
        String hAmount = help_other.getAmount();
        int hType = help_other.getType();
        if (!"".equals(hUserId) && hName != null && !"".equals(hName) && hSex != null && !"".equals(hSex) && homeTown != null && !"".equals(homeTown) && hID != null && !"".equals(hID) && hPhone != null && !"".equals(hPhone) && hAddress != null && !"".equals(hAddress) && hReason != null && !"".equals(hReason) && hAmount != null && !"".equals(hAmount) &&  !"".equals(hType)) {
            return true;
        } else {
            return false;
        }

    }

}
